
#include "xn297l.h"
#include "spi.h"
#include "gpio.h"
#include "hc32_ll_gpio.h"
#include "hc32_common.h"
#include "hc32_ll_utility.h"
#include "baseData.h"
#include "dma.h"	 
#include <stdio.h>	 
	 
#include "FreeRTOS.h"
#include "task.h"	

uint8_t rx_address[5] =  {0x00,0x00,0x00,0xcc,0xcc};
						
Devce_e Devce;  		
uint8_t xn297lSendData[32];
uint8_t tx_address0[5] = {0x00,0x00,0x00,0xcc,0xcc};
uint8_t tx_address1[5] = {0x11,0x11,0x11,0xcc,0xcc};
uint8_t tx_address2[5] = {0x22,0x22,0x22,0xcc,0xcc};
uint8_t tx_address3[5] = {0x33,0x33,0x33,0xcc,0xcc};
uint8_t tx_address4[5] = {0x44,0x44,0x44,0xcc,0xcc};
uint8_t tx_address5[5] = {0x55,0x55,0x55,0xcc,0xcc};

void setXn297lAddress(void)
{
	xn297lWriteBuf(W_REGISTER + TX_ADDR, rx_address, sizeof(rx_address));
	xn297lWriteBuf(W_REGISTER + RX_ADDR_P0,rx_address, sizeof(rx_address));
}

void xn297lRxData(void)
{
	if(READ_IRQ == RESET)
	{
 
		xn297lWriteBuf(W_ACK_PAYLOAD, sendToPtxXn297l,PAYLOAD_WIDTH);		//射频发送 
		xn297lReadBuf(R_RX_PAYLOAD, receivedFromPtxXn297l, PAYLOAD_WIDTH);	//射频接收 
		//串口发送(no add)
		
		xn297lWriteReg(W_REGISTER + STATUS, 0x70);    
		vTaskDelay(500); //
	}
}

/*
 *初始化 xn297l 
 */
static void regConfig(uint8_t *addr)
{
    uint8_t BB_cal_data[] = {0x2a, 0xec, 0x6f, 0x9c, 0x46};
    uint8_t RF_cal_data[] = {0xf6, 0x37, 0x5d};
    uint8_t RF_cal2_data[] = {0xd5, 0x21, 0xef, 0x2c, 0x5a, 0x40};
    uint8_t Dem_cal_data[] = {0x01};
    uint8_t Dem_cal2_data[] = {0x0b, 0xdf, 0x02};

    CE_L;
    xn297lWriteReg(RST_FSPI, 0x5A); // Software Reset
    xn297lWriteReg(RST_FSPI, 0XA5);
    xn297lWriteReg(FLUSH_RX, 0);                           // CLEAR  RXFIFO
    xn297lWriteReg(FLUSH_TX, 0);                           // CLEAR TXFIFO
    xn297lWriteReg(W_REGISTER + STATUS, 0x70);             // CLEAR  STATUS
    xn297lWriteReg(W_REGISTER + EN_RXADDR, 0x01);          // Enable Pipe0
    xn297lWriteReg(W_REGISTER + SETUP_AW, 0x03);           // address witdth is 5 bytes
    xn297lWriteReg(W_REGISTER + RF_CH, DEFAULT_CHANNEL);   // 2478M HZ
    xn297lWriteReg(W_REGISTER + RX_PW_P0, PAYLOAD_WIDTH);  // 32 bytes
    // 初始化本机地址
    xn297lWriteBuf(W_REGISTER + TX_ADDR, addr, 5);    // Writes TX_Address to PN006
    xn297lWriteBuf(W_REGISTER + RX_ADDR_P0, addr, 5); // RX_Addr0 same as TX_Adr for Auto.Ack

    xn297lWriteBuf(W_REGISTER + BB_CAL, BB_cal_data, sizeof(BB_cal_data));
    xn297lWriteBuf(W_REGISTER + RF_CAL2, RF_cal2_data, sizeof(RF_cal2_data));
    xn297lWriteBuf(W_REGISTER + DEM_CAL, Dem_cal_data, sizeof(Dem_cal_data));
    xn297lWriteBuf(W_REGISTER + RF_CAL, RF_cal_data, sizeof(RF_cal_data));
    xn297lWriteBuf(W_REGISTER + DEM_CAL2, Dem_cal2_data, sizeof(Dem_cal2_data));

    xn297lWriteReg(W_REGISTER + DYNPD, 0x00);
    xn297lWriteReg(W_REGISTER + RF_SETUP, RF_POWER); // DBM
    
	if(TRANSMIT_TYPE == TRANS_ENHANCE_MODE)      	
    {
		xn297lWriteReg(ACTIVATE, 0x73);                //如果使用W_ACK则需要这句
		xn297lWriteReg(W_REGISTER + SETUP_RETR, 0x01); //  3 retrans...
		xn297lWriteReg(W_REGISTER + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
		xn297lWriteReg(W_REGISTER + FEATURE, 0x02);
    }
    else if(TRANSMIT_TYPE == TRANS_BURST_MODE)
    {
        xn297lWriteReg(W_REGISTER + SETUP_RETR,0x00);// Disable retrans... 	
        xn297lWriteReg(W_REGISTER + EN_AA,     0x00);// Disable AutoAck 
        xn297lWriteReg(W_REGISTER + FEATURE, 0x00);
    }
	
    //xn297l通信模式设置
    if(TRANS_MODE == TX)
    {
        xn297lWriteReg(W_REGISTER + CONFIG,  0XCE);     // 将RF设置成TX模式
    }
    else
    {
        xn297lWriteReg(W_REGISTER + CONFIG,  0XBF );    // 将RF设置成RX模式   
    }
    CE_H; //拉高CE,配置完毕，使能开始
    DDL_DelayMS(5);
}

/*
 *判断芯片是否存在 
 *初始化 xn297l
 */
void xn297lInit(void)
{
	spiInit();
    gpioSpiConfig();
    // 检测 xn297l 是否连接
    uint8_t t_regVal = xn297lReadReg(0x0f);
    if(0xc6 == t_regVal)
    {
        uint8_t default_addr[] = {1,2,3,4,5};
        regConfig(default_addr); 
		Devce.devceExist=1;
		printf("\r\nxn297l CORRECT \r\n");
    }
	else
	{
		Devce.devceExist=0;
		printf("\r\nxn297l error \r\n");
	}
}

/*
 *Input: 新通道值
 *Description: 改变收发通道（0~78）
 *Return: void
 */
void xn297lSetChannel(uint8_t t_channel)
{
    xn297lWriteReg(W_REGISTER + RF_CH, t_channel);
}

// 设置地址
void xn297lSetAddr(uint8_t *t_addr)
{
    regConfig(t_addr);
}

void xn297lClose(void)
{
    CE_L;
}

void xn297lOn(void)
{
    CE_H;
}
